`:top
A `!syntax error`! is a mismatch in the `F33f`_`[syntax`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Syntax_(programming_languages)]`_`f of `F33f`_`[data`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data]`_`f `F33f`_`[input`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Input_data]`_`f to a `F33f`_`[computer system`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_system]`_`f that requires a specific syntax. For `F33f`_`[source code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Source_code]`_`f in a `F33f`_`[programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f, a `F33f`_`[compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compiler]`_`f detects syntax errors before the software is run; at compile-time, whereas an `F33f`_`[interpreter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Interpreter]`_`f detects syntax errors at `F33f`_`[run-time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Run_time_(program_lifecycle_phase)]`_`f. A syntax error can occur based on syntax rules other than those defined by a programming language. For example, typing an invalid equation into a calculator (an interpreter) is a syntax error.
Some errors that occur during the translation of source code may be considered syntax errors by some but not by others. For example, some say that an uninitialized variable in `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f is a syntax error, but others disagree`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f]`:cite-ref-uninitialized-var-2-0[`F5bf`_`[2`#cite-note-uninitialized-var-2]`_`f] – classifying it as a `F33f`_`[static semantic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f error.`:cite-ref-uninitialized-var-2-1[`F5bf`_`[2`#cite-note-uninitialized-var-2]`_`f]`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f]`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f]
>>Contents
• `F0af`_`[Examples`#examples]`_`f
• `F0af`_`[In Java`#in-java]`_`f
• `F0af`_`[In Lisp`#in-lisp]`_`f
• `F0af`_`[In Python`#in-python]`_`f
• `F0af`_`[On a calculator`#on-a-calculator]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[References`#references]`_`f
-─
>>Examples
>>>In Java
The Java compiler generates a syntax error for the following code since the string is not quoted. The compilation process fails and does not produce a usable executable.
`B100`F9d9System.out.println(Hello World);`f`b
Valid syntax is:
\<syntax highlighting lang="java"> System.out.println("Hello World"); </syntax highlighting >
>>>In Lisp
The code `B100`F9d9(add 1 1)`f`b is a syntactically valid `F33f`_`[Lisp`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lisp_(programming_language)]`_`f program (assuming the 'add' function exists) that adds 1 and 1.
However, `B100`F9d9(_ 1 1)`f`b results in syntax error `B100`F9d9lexical error: '_' is not valid`f`b. The lexer is unable to identify the first error – all it knows is that, after producing the token LEFT_PAREN, '(' the remainder of the program is invalid, since no word rule begins with '_'. And, `B100`F9d9(add 1 1`f`b results in syntax error `B100`F9d9parsing error: missing closing ')'`f`b. The parser identifies the "list" production rule due to the '(' token (as the only match), and thus gives an error message; in general it may be `F33f`_`[ambiguous grammar`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ambiguous_grammar]`_`f.
Type errors and undeclared variable errors are sometimes considered to be syntax errors when they are detected at compile-time (which is usually the case when compiling strongly-typed languages), though it is common to classify these kinds of error as `F33f`_`[semantic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f errors instead.`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f]`:cite-ref-6[`F5bf`_`[6`#cite-note-6]`_`f]`:cite-ref-uninitialized-var-2-2[`F5bf`_`[2`#cite-note-uninitialized-var-2]`_`f]
>>>In Python
For `F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f code, `B100`F9d9'a' + 1`f`b contains a type error because it adds a string literal to an integer literal. A type error like this can be detected at compile-time – during parsing (phrase analysis) – if the compiler uses separate rules that allow "`*integer-literal`* + `*integer-literal`*" but not "`*string-literal`* + `*integer-literal`*", though it is more likely that the compiler will use a parsing rule that allows expressions of the form "`*literal-or-identifier`* + `*literal-or-identifier`*" and then the error will be detected during contextual analysis (when type checking occurs). In some cases, this validation is not done by the compiler, and these errors are only detected at runtime.
In a dynamically typed language, where type can only be determined at runtime, many type errors can only be detected at runtime. For example, for Python `B100`F9d9a + b`f`b is syntactically valid at the phrase level, but the correctness of the types of a and b can only be determined at runtime, as variables do not have types in Python, only values do. Whereas there is disagreement about whether a type error detected by the compiler should be called a syntax error (static semantic), type errors which can only be detected at program execution time are always regarded as semantic rather than syntax errors.
>>>On a calculator
A syntax error can occur on a `F33f`_`[calculator`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Calculator]`_`f (especially a `F33f`_`[scientific`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scientific_calculator]`_`f or `F33f`_`[graphing`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Graphing_calculator]`_`f calculator) when the input `F33f`_`[equation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Equation]`_`f is incorrect in ways such as:
• Invalid number or operation
• Open `F33f`_`[bracket`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bracket]`_`f without closing
• Using `F33f`_`[minus sign`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Minus_sign]`_`f instead of negative symbol (or vice versa)
>>See also
• `F33f`_`[Tag soup`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Tag_soup]`_`f
>>References
`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f Issue of syntax or semantics?
`:cite-note-uninitialized-var-2`!2.`! `F0af`_`[↑`#cite-ref-uninitialized-var-2-0]`_`f Semantic Errors in Java
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f `:citerefahomonica-s-lamravi-sethijeffrey-d-ullman2007`aAho, Alfred V.; Monica S. Lam; Ravi Sethi; Jeffrey D. Ullman (2007). `*Compilers: Principles, Techniques, and Tools`* (2nd ed.). Addison Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0-321-48681-3. Section 4.1.3: Syntax Error Handling, pp.194–195.
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f `:citereflouden1997`aLouden, Kenneth C. (1997). `*Compiler Construction: Principles and Practice`*. Brooks/Cole. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 981-243-694-4. Exercise 1.3, pp.27–28.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f `:citerefahomonica-s-lamravi-sethijeffrey-d-ullman2007`aAho, Alfred V.; Monica S. Lam; Ravi Sethi; Jeffrey D. Ullman (2007). `*Compilers: Principles, Techniques, and Tools`* (2nd ed.). Addison Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0-321-48681-3.Section 4.1.3: Syntax Error Handling, pp.194–195.
`:cite-note-6`!6.`! `F0af`_`[↑`#cite-ref-6]`_`f `:citereflouden1997`aLouden, Kenneth C. (1997). `*Compiler Construction: Principles and Practice`*. Brooks/Cole. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 981-243-694-4. Exercise 1.3, pp.27–28.
`c`F0af`_`[↑ Back to top`#top]`_`f`a